<?php

namespace app\common\lib;

use think\facade\Db;

class MakeTable
{
    public function run($dalei,$table = ''){
        $initData = $this->initData($dalei);
        if(empty($initData)){
            return null;
        }

        if($table == 'cost'){
            $data = $this->makeCost($dalei,$initData['dpmj'],$initData['fzfy'],$initData['cdzx'],$initData['sbfy'],$initData['cpwl'],$initData['rygz'],$initData['sdzf'],$initData['xcfy']);
        }elseif($table == 'profit'){
            $data = $this->makeProfit($dalei,$initData['dpmj'],$initData['fzfy'],$initData['sdzf'],$initData['rjxf'],$initData['rkll'],$initData['mll']);
        }else{
            $data['cost'] = $this->makeCost($dalei,$initData['dpmj'],$initData['fzfy'],$initData['cdzx'],$initData['sbfy'],$initData['cpwl'],$initData['rygz'],$initData['sdzf'],$initData['xcfy']);
            $data['profit'] = $this->makeProfit($dalei,$initData['dpmj'],$initData['fzfy'],$initData['sdzf'],$initData['rjxf'],$initData['rkll'],$initData['mll']);
        }
        
        return $data;
    }

    //获取表格
    public function getTable($initData,$dalei,$table = ''){
        if(empty($initData)){
            return null;
        }
        if($table == 'cost'){
            $data = $this->makeCost($dalei,$initData['dpmj'],$initData['fzfy'],$initData['cdzx'],$initData['sbfy'],$initData['cpwl'],$initData['rygz'],$initData['sdzf'],$initData['xcfy']);
        }elseif($table == 'profit'){
            $data = $this->makeProfit($dalei,$initData['dpmj'],$initData['fzfy'],$initData['sdzf'],$initData['rjxf'],$initData['rkll'],$initData['mll']);
        }else{
            $data['cost'] = $this->makeCost($dalei,$initData['dpmj'],$initData['fzfy'],$initData['cdzx'],$initData['sbfy'],$initData['cpwl'],$initData['rygz'],$initData['sdzf'],$initData['xcfy']);
            $data['profit'] = $this->makeProfit($dalei,$initData['dpmj'],$initData['fzfy'],$initData['sdzf'],$initData['rjxf'],$initData['rkll'],$initData['mll']);
        }
        return $data;
    }

    
    //初始数据
    public function initData($dalei){
        switch($dalei){
            case '饮品':
                //店铺面积
                $dpmjMin = 25; $dpmjMax = 40; $dpmjStep = 5;
                //设备费用
                $sbfyMin = 15000; $sbfyMax = 30000; $sbfyStep = 1000;
                //产品物料
                $cpwlMin = 10000; $cpwlMax = 30000; $cpwlStep = 1000;
                //人员工资
                $rygzMin = 7000; $rygzMax = 9000; $rygzStep = 500;
                //水电杂费
                $sdzfMin = 1000; $sdzfMax = 1500; $sdzfStep = 100;
                //宣传费用
                $xcfyMin = 3000; $xcfyMax = 5000; $xcfyStep = 1000;

                //人均消费
                $rjxfMin = 10; $rjxfMax = 20;
                //日客流量
                $rkllMin = 150; $rkllMax = 260; $rkllStep = 10;
                //毛利率
                $mllMin = 50; $mllMax = 65;
                break;
            case '小吃':
                $dpmjMin = 20; $dpmjMax = 40; $dpmjStep = 5;
                $sbfyMin = 15000; $sbfyMax = 30000; $sbfyStep = 1000;
                $cpwlMin = 15000; $cpwlMax = 30000; $cpwlStep = 1000;
                $rygzMin = 7000; $rygzMax = 9000; $rygzStep = 500;
                $sdzfMin = 1000; $sdzfMax = 1500; $sdzfStep = 100;
                $xcfyMin = 3000; $xcfyMax = 5000; $xcfyStep = 1000;

                $rjxfMin = 10; $rjxfMax = 20;
                $rkllMin = 150; $rkllMax = 260; $rkllStep = 10;
                $mllMin = 50; $mllMax = 65;
                break;
            case '甜品':
                $dpmjMin = 30; $dpmjMax = 60; $dpmjStep = 5;
                $sbfyMin = 20000; $sbfyMax = 40000; $sbfyStep = 1000;
                $cpwlMin = 15000; $cpwlMax = 30000; $cpwlStep = 1000;
                $rygzMin = 7000; $rygzMax = 9000; $rygzStep = 500;
                $sdzfMin = 1000; $sdzfMax = 1500; $sdzfStep = 100;
                $xcfyMin = 3000; $xcfyMax = 5000; $xcfyStep = 1000;

                $rjxfMin = 15; $rjxfMax = 40;
                $rkllMin = 100; $rkllMax = 220; $rkllStep = 10;
                $mllMin = 60; $mllMax = 75;
                break;
            case '快餐':
                $dpmjMin = 30; $dpmjMax = 60; $dpmjStep = 5;
                $sbfyMin = 20000; $sbfyMax = 40000; $sbfyStep = 1000;
                $cpwlMin = 20000; $cpwlMax = 40000; $cpwlStep = 1000;
                $rygzMin = 7000; $rygzMax = 9000; $rygzStep = 500;
                $sdzfMin = 1200; $sdzfMax = 2000; $sdzfStep = 100;
                $xcfyMin = 3000; $xcfyMax = 5000; $xcfyStep = 1000;

                $rjxfMin = 15; $rjxfMax = 30;
                $rkllMin = 100; $rkllMax = 220; $rkllStep = 10;
                $mllMin = 50; $mllMax = 60;
                break;
            case '日韩料理':
                $dpmjMin = 25; $dpmjMax = 60; $dpmjStep = 5;
                $sbfyMin = 15000; $sbfyMax = 30000; $sbfyStep = 1000;
                $cpwlMin = 15000; $cpwlMax = 30000; $cpwlStep = 1000;
                $rygzMin = 7000; $rygzMax = 9000; $rygzStep = 500;
                $sdzfMin = 1000; $sdzfMax = 1500; $sdzfStep = 100;
                $xcfyMin = 3000; $xcfyMax = 5000; $xcfyStep = 1000;

                $rjxfMin = 30; $rjxfMax = 80;
                $rkllMin = 100; $rkllMax = 180; $rkllStep = 10;
                $mllMin = 55; $mllMax = 70;
                break;
            case '火锅':
                $dpmjMin = 120; $dpmjMax = 200; $dpmjStep = 10;
                $sbfyMin = 30000; $sbfyMax = 60000; $sbfyStep = 1000;
                $cpwlMin = 20000; $cpwlMax = 40000; $cpwlStep = 1000;
                $rygzMin = 9000; $rygzMax = 15000; $rygzStep = 1000;
                $sdzfMin = 2500; $sdzfMax = 4000; $sdzfStep = 100;
                $xcfyMin = 5000; $xcfyMax = 8000; $xcfyStep = 1000;

                $rjxfMin = 60; $rjxfMax = 160;
                $rkllMin = 100; $rkllMax = 180; $rkllStep = 10;
                $mllMin = 50; $mllMax = 70;
                break;
            case '面食':
                $dpmjMin = 30; $dpmjMax = 60; $dpmjStep = 5;
                $sbfyMin = 20000; $sbfyMax = 40000; $sbfyStep = 1000;
                $cpwlMin = 15000; $cpwlMax = 30000; $cpwlStep = 1000;
                $rygzMin = 6000; $rygzMax = 9000; $rygzStep = 500;
                $sdzfMin = 1200; $sdzfMax = 2000; $sdzfStep = 100;
                $xcfyMin = 3000; $xcfyMax = 5000; $xcfyStep = 1000;

                $rjxfMin = 15; $rjxfMax = 30;
                $rkllMin = 100; $rkllMax = 220; $rkllStep = 10;
                $mllMin = 50; $mllMax = 65;
                break;
            default:
                return null;
                break;
        }
        //店铺面积
        $data['dpmj'] = $this->randomNum($dpmjMin,$dpmjMax,$dpmjStep);
        //房租费用
        $data['fzfy'] = $this->randomNum(120,300,10);
        //场地装修
        $data['cdzx'] = $this->randomNum(800,1500,100);
        //设备费用
        $data['sbfy'] = $this->randomNum($sbfyMin,$sbfyMax,$sbfyStep);
        //产品物料
        $data['cpwl'] = $this->randomNum($cpwlMin,$cpwlMax,$cpwlStep);
        //人员工资
        $data['rygz'] = $this->randomNum($rygzMin,$rygzMax,$rygzStep);
        //水电杂费
        $data['sdzf'] = $this->randomNum($sdzfMin,$sdzfMax,$sdzfStep);
        //宣传费用
        $data['xcfy'] = $this->randomNum($xcfyMin,$xcfyMax,$xcfyStep);
        
        //人均消费
        $data['rjxf'] = mt_rand($rjxfMin,$rjxfMax);
        //日客流量
        $data['rkll'] = $this->randomNum($rkllMin,$rkllMax,$rkllStep);
        //毛利率
        $data['mll'] = mt_rand($mllMin,$mllMax);

        $fz = $data['fzfy'] * $data['dpmj'];
        $zx = $data['cdzx'] * $data['dpmj'];
        $data['ldzj'] = $fz*3 + $data['rygz']*3;
        $data['total'] = $fz + $zx + $data['sbfy'] + $data['cpwl'] + $data['rygz'] + $data['sdzf'];
        return $data;
    }
    
    private function makeCost($dalei,$dpmj,$fzfy,$cdzx,$sbfy,$cpwl,$rygz,$sdzf,$xcfy){
        switch($dalei){
            case '饮品':
                $dpmjDec = 5; $rygzDec = 2000; $sdzfDec = 200; $xcfyDec = 500;
                break;
            case '小吃':
                $dpmjDec = 5; $rygzDec = 2000; $sdzfDec = 200; $xcfyDec = 500;
                break;
            case '甜品':
                $dpmjDec = $dpmj < 40 ? 5 : 10; $rygzDec = 2000; $sdzfDec = 200; $xcfyDec = 500;
                break;
            case '快餐':
                $dpmjDec = $dpmj < 40 ? 5 : 10; $rygzDec = 2000; $sdzfDec = 200; $xcfyDec = 500;
                break;
            case '日韩料理':
                $dpmjDec = $dpmj < 40 ? 5 : 10; $rygzDec = 2000; $sdzfDec = 200; $xcfyDec = 500;
                break;
            case '火锅':
                $dpmjDec = $dpmj < 150 ? 20 : 30; $rygzDec = 2000; $sdzfDec = 300; $xcfyDec = 500;
                break;
            case '面食':
                $dpmjDec = $dpmj < 40 ? 5 : 10; $rygzDec = 2000; $sdzfDec = 200; $xcfyDec = 500;
                break;
        }
        //总计
        $fz = $fzfy * $dpmj;
        $zx = $cdzx * $dpmj;
        $ldzj = $fz*3 + $rygz*3;
        $total = $fz + $zx + $sbfy + $cpwl + $rygz + $sdzf;
    
        $dpmj2 = $dpmj - $dpmjDec;
        $fz2 = $fzfy * $dpmj2;
        $zx2 = $cdzx * $dpmj2;
        $sbfy2 = $sbfy;
        $cpwl2 = $cpwl;
        $rygz2 = $rygz - $rygzDec;
        $sdzf2 = $sdzf - $sdzfDec;
        $xcfy2 = $xcfy - $xcfyDec;
        $ldzj2 = $fz2*3 + $rygz2*3;
        $total2 = $fz2 + $zx2 + $sbfy2 + $cpwl2 + $rygz2 + $sdzf2;
    
        $dpmj3 = $dpmj2 - $dpmjDec;
        $fz3 = $fzfy * $dpmj3;
        $zx3 = $cdzx * $dpmj3;
        $sbfy3 = $sbfy;
        $cpwl3 = $cpwl;
        $rygz3 = $rygz2 - $rygzDec;
        $sdzf3 = $sdzf2 - $sdzfDec;
        $xcfy3 = $xcfy2 - $xcfyDec;
        $ldzj3 = $fz3*3 + $rygz3*3;
        $total3 = $fz3 + $zx3 + $sbfy3 + $cpwl3 + $rygz3 + $sdzf3;
    
        $html = '<table class="money_table">
                    <tr>
                        <th>店铺类型</th>
                        <th>旗舰店</th>
                        <th>标准店</th>
                        <th>创业店</th>
                    </tr>
                    <tr>
                        <td>店铺面积</td>
                        <td>'.$dpmj.'㎡</td>
                        <td>'.$dpmj2.'㎡</td>
                        <td>'.$dpmj3.'㎡</td>
                    </tr>
                    <tr>
                        <td>房租费用</td>
                        <td>'.$fz.'元/月</td>
                        <td>'.$fz2.'元/月</td>
                        <td>'.$fz3.'元/月</td>
                    </tr>
                    <tr>
                        <td>场地装修</td>
                        <td>'.round($zx/10000,2).'万元</td>
                        <td>'.round($zx2/10000,2).'万元</td>
                        <td>'.round($zx3/10000,2).'万元</td>
                    </tr>
                    <tr>
                        <td>设备费用</td>
                        <td>'.round($sbfy/10000,2).'万元</td>
                        <td>'.round($sbfy2/10000,2).'万元</td>
                        <td>'.round($sbfy3/10000,2).'万元</td>
                    </tr>
                    <tr>
                        <td>产品物料</td>
                        <td>'.round($cpwl/10000,2).'万元</td>
                        <td>'.round($cpwl2/10000,2).'万元</td>
                        <td>'.round($cpwl3/10000,2).'万元</td>
                    </tr>
                    <tr>
                        <td>人员工资</td>
                        <td>'.$rygz.'元/月</td>
                        <td>'.$rygz2.'元/月</td>
                        <td>'.$rygz3.'元/月</td>
                    </tr>
                    <tr>
                        <td>水电杂费</td>
                        <td>'.$sdzf.'元/月</td>
                        <td>'.$sdzf2.'元/月</td>
                        <td>'.$sdzf3.'元/月</td>
                    </tr>
                    <tr>
                        <td>宣传费用</td>
                        <td>'.$xcfy.'元</td>
                        <td>'.$xcfy2.'元</td>
                        <td>'.$xcfy3.'元</td>
                    </tr>
                    <tr>
                        <td>流动资金</td>
                        <td>'.round($ldzj/10000,2).'万元</td>
                        <td>'.round($ldzj2/10000,2).'万元</td>
                        <td>'.round($ldzj3/10000,2).'万元</td>
                    </tr>
                    <tr>
                        <td>总计预估费用</td>
                        <td>'.round($total/10000,2).'万元</td>
                        <td>'.round($total2/10000,2).'万元</td>
                        <td>'.round($total3/10000,2).'万元</td>
                    </tr>
                </table>';
        return $html;
    }

    //利润
    private function makeProfit($dalei,$dpmj,$fzfy,$sdzf,$rjxf,$rkll,$mll){
        switch($dalei){
            case '饮品':
                $dpmjDec = 5; $sdzfDec = 200;
                if($rkll < 190){
                    $rkll2 = $rkll - 20;
                    $rkll3 = $rkll2 - 10;
                }else{
                    $rkll2 = $rkll - mt_rand(30,60);
                    $rkll3 = $rkll2 - 10;
                }
                break;
            case '小吃':
                $dpmjDec = 5; $sdzfDec = 200;
                if($rkll < 190){
                    $rkll2 = $rkll - 20;
                    $rkll3 = $rkll2 - 10;
                }else{
                    $rkll2 = $rkll - mt_rand(30,60);
                    $rkll3 = $rkll2 - 10;
                }
                break;
            case '甜品':
                $dpmjDec = $dpmj < 40 ? 5 : 10; $sdzfDec = 200;
                if($rkll < 160){
                    $rkll2 = $rkll - 20;
                    $rkll3 = $rkll2 - 10;
                }else{
                    $rkll2 = $rkll - mt_rand(25,40);
                    $rkll3 = $rkll2 - 10;
                }
                break;
            case '快餐':
                $dpmjDec = $dpmj < 40 ? 5 : 10; $sdzfDec = 200;
                if($rkll < 160){
                    $rkll2 = $rkll - 20;
                    $rkll3 = $rkll2 - 10;
                }else{
                    $rkll2 = $rkll - mt_rand(25,40);
                    $rkll3 = $rkll2 - 10;
                }
                break;
            case '日韩料理':
                $dpmjDec = $dpmj < 40 ? 5 : 10; $sdzfDec = 200;
                if($rkll < 130){
                    $rkll2 = $rkll - 20;
                    $rkll3 = $rkll2 - 10;
                }else{
                    $rkll2 = $rkll - mt_rand(20,40);
                    $rkll3 = $rkll2 - 10;
                }
                break;
            case '火锅':
                $dpmjDec = $dpmj < 150 ? 20 : 30; $sdzfDec = 300;
                if($rkll < 130){
                    $rkll2 = $rkll - 20;
                    $rkll3 = $rkll2 - 10;
                }else{
                    $rkll2 = $rkll - mt_rand(20,40);
                    $rkll3 = $rkll2 - 10;
                }
                break;
            case '面食':
                $dpmjDec = $dpmj < 40 ? 5 : 10; $sdzfDec = 200;
                if($rkll < 160){
                    $rkll2 = $rkll - 20;
                    $rkll3 = $rkll2 - 10;
                }else{
                    $rkll2 = $rkll - mt_rand(25,40);
                    $rkll3 = $rkll2 - 10;
                }
                break;
        }
        $ryye = $rjxf * $rkll;
        $yyye = $ryye * 30;
        $ymlr = $yyye * $mll / 100;
        $fz = $fzfy * $dpmj;
        $yjlr = $ymlr - $fz - $sdzf;
        $njlr = $yjlr * 12;

        $dpmj2 = $dpmj - $dpmjDec;
        $ryye2 = $rjxf * $rkll2;
        $yyye2 = $ryye2 * 30;
        $ymlr2 = $yyye2 * $mll / 100;
        $fz2 = $fzfy * $dpmj2;
        $sdzf2 = $sdzf - $sdzfDec;
        $yjlr2 = $ymlr2 - $fz2 - $sdzf2;
        $njlr2 = $yjlr2 * 12;

        $dpmj3 = $dpmj2 - $dpmjDec;
        $ryye3 = $rjxf * $rkll3;
        $yyye3 = $ryye3 * 30;
        $ymlr3 = $yyye3 * $mll / 100;
        $fz3 = $fzfy * $dpmj3;
        $sdzf3 = $sdzf2 - $sdzfDec;
        $yjlr3 = $ymlr3 - $fz3 - $sdzf3;
        $njlr3 = $yjlr3 * 12;

        $html = '<table class="money_table">
                    <tr>
                        <th>店铺类型</th>
                        <th>旗舰店</th>
                        <th>标准店</th>
                        <th>创业店</th>
                    </tr>
                    <tr>
                        <td>店铺面积</td>
                        <td>'.$dpmj.'㎡</td>
                        <td>'.$dpmj2.'㎡</td>
                        <td>'.$dpmj3.'㎡</td>
                    </tr>
                    <tr>
                        <td>人均消费</td>
                        <td>'.$rjxf.'元</td>
                        <td>'.$rjxf.'元</td>
                        <td>'.$rjxf.'元</td>
                    </tr>
                    <tr>
                        <td>客流量/天</td>
                        <td>'.$rkll.'人</td>
                        <td>'.$rkll2.'人</td>
                        <td>'.$rkll3.'人</td>
                    </tr>
                    <tr>
                        <td>营业额/天</td>
                        <td>'.$ryye.'元</td>
                        <td>'.$ryye2.'元</td>
                        <td>'.$ryye3.'元</td>
                    </tr>
                    <tr>
                        <td>营业额/月</td>
                        <td>'.round($yyye/10000,2).'万元</td>
                        <td>'.round($yyye2/10000,2).'万元</td>
                        <td>'.round($yyye3/10000,2).'万元</td>
                    </tr>
                    <tr>
                        <td>毛利率</td>
                        <td>'.$mll.'%</td>
                        <td>'.$mll.'%</td>
                        <td>'.$mll.'%</td>
                    </tr>
                    <tr>
                        <td>毛利润/月</td>
                        <td>'.round($ymlr/10000,2).'万元</td>
                        <td>'.round($ymlr2/10000,2).'万元</td>
                        <td>'.round($ymlr3/10000,2).'万元</td>
                    </tr>
                    <tr>
                        <td>房租费用/月</td>
                        <td>'.$fz.'元</td>
                        <td>'.$fz2.'元</td>
                        <td>'.$fz3.'元</td>
                    </tr>
                    <tr>
                        <td>水电杂费/月</td>
                        <td>'.$sdzf.'元</td>
                        <td>'.$sdzf2.'元</td>
                        <td>'.$sdzf3.'元</td>
                    </tr>
                    <tr>
                        <td>月净利润</td>
                        <td>'.round($yjlr/10000,2).'万元</td>
                        <td>'.round($yjlr2/10000,2).'万元</td>
                        <td>'.round($yjlr3/10000,2).'万元</td>
                    </tr>
                    <tr>
                        <td>年净利润</td>
                        <td>'.round($njlr/10000,2).'万元</td>
                        <td>'.round($njlr2/10000,2).'万元</td>
                        <td>'.round($njlr3/10000,2).'万元</td>
                    </tr>
                </table>';
        return $html;
    }

    //随机数
    private function randomNum($min,$max,$step = 5){
        $num = mt_rand($min, ($max + $step));
        $num = $num - ($num % $step);
        return $num;
    }
    

}
